package demo

import _root_.org.mortbay.jetty.Server
import _root_.org.mortbay.jetty.webapp.WebAppContext
import org.mortbay.jetty.nio._
import tools.nsc.io.Path
import java.io.FileNotFoundException

object DemoWebApp
{
  def main(args: Array[String])
  {
    System.setProperty("run.mode", "test")
    val server = new Server
    val scc = new SelectChannelConnector
    scc.setPort(8080)
    server.setConnectors(Array(scc))

    val context = new WebAppContext()
    context.setServer(server)
    context.setContextPath("/")

    val dirsToCheck = List("../src/main/webapp", "smm-web/src/main/webapp")
    val path = dirsToCheck.find(Path(_).exists).getOrElse(throw new FileNotFoundException("Path to webapp not found"))

    context.setWar(path)

    server.addHandler(context)

    try {
      println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP")
      server.start()
      while (System.in.available() == 0) {
        Thread.sleep(5000)
      }
      server.stop()
      server.join()
    } catch {
      case exc: Exception => {
        exc.printStackTrace()
        System.exit(100)
      }
    }
  }
}
